package com.mecuros.handler.deduplication.builder;

import com.alibaba.fastjson.JSONObject;
import com.mecuros.common.domain.TaskInfo;
import com.mecuros.handler.deduplication.DeduplicationHolder;
import com.mecuros.handler.domain.DeduplicationParam;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;

/**
 * @Classname AbstractDeduplicationBuilder
 * @Description
 * @Date 2022/5/22 9:17 下午
 * @Created by yujianbo
 */
public abstract class AbstractDeduplicationBuilder implements Builder {

    protected Integer deduplicationType;

    @Autowired
    private DeduplicationHolder deduplicationHolder;

    @PostConstruct
    public void init(){
        deduplicationHolder.putBuilder(deduplicationType,this);
    }


    public DeduplicationParam getParamsFromConfig(Integer key, String duplicationConfig, TaskInfo taskInfo) {
        JSONObject object = JSONObject.parseObject(duplicationConfig);
        if (object == null) {
            return null;
        }
        DeduplicationParam deduplicationParam = JSONObject.parseObject(object.getString(DEDUPLICATION_CONFIG_PRE + key), DeduplicationParam.class);
        if (deduplicationParam == null) {
            return null;
        }
        deduplicationParam.setTaskInfo(taskInfo);
        return deduplicationParam;
    }
}
